﻿using Minecraft.BLL;
using Minecraft.BLL.mysql;
using Minecraft.Config;
using Minecraft.Config.Csv.CsvCls;
using Minecraft.Config.Csv.CsvCom;
using Minecraft.Config.ipConst;
using Minecraft.Model;
using Minecraft.Model.ReqResp;
using Minecraft.Model.Treasurebox;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Minecraft.ServerHall.Cmd
{
	public class TreasureboxListSelect : CommandBase<MinecraftSession, StringRequestInfo>
	{
		public override string Name
		{
			get
			{
				return ProtocolHelper.GetProtocolStr(defCommand);
			}
		}
		private EnumCommand defCommand = EnumCommand.Treasurebox_TreasureboxListSelect;
		public override void ExecuteCommand(MinecraftSession session, StringRequestInfo requestInfo)
		{
			var req = requestInfo.GetRequestObj<TreasureboxListSelectReq>(session);
			if (req == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：请求实体为空）" });
				return;
			}
			if (!session.minecraftSessionInfo.IsLogin)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：未登录）" });
				return;
			}
			if (req.mapId.IsNullOrWhiteSpace())
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：mapId 参数为空）" });
				return;
			}
			if (req.mapId == MinecraftConfig.BaseMapId)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：mapId 为基地宝箱信息，暂不支持查询）" });
				return;
			}
			//var mapInfo = CsvConfig.mapList.FirstOrDefault(m => m.Id == req.mapId);
			var mapInfo = CsvConfig.GetMod_MapInfoById(req.mapId);
			if (mapInfo == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：地图信息不存在【req.mapId：{req.mapId}】）" });
				return;
			}
			var map = session.minecraftSessionInfo.map;

			if (map.SourceMapId != mapInfo.Id)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：玩家不在当前地图，不允许操作【SourceMapId：{map.SourceMapId}，mapId：{mapInfo.Id}】）" });
				return;
			}
			//随机宝箱
			var treasureBoxList = session.minecraftSessionInfo.furnitureTreasureBoxList.Where(m => m.TreasureBox_MapId == req.mapId && m.TreasureBox_IsPrefab_Convert == false).ToList();
			//if (treasureBoxList == null || !treasureBoxList.Any())
			//{
			//	session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：玩家在该地图没有宝箱生成，错误原因可能是服务器的逻辑错误，或者时前端接口调用顺序错误）" });
			//	return;
			//}

			List<TreasureboxSelectItemInfo> treasureboxListSelectItemInfoList = treasureBoxList.Select(m => new TreasureboxSelectItemInfo
			{
				treasureboxId = m.FurnitureId,
				itemId = m.FurnitureItemId,
				resourceId = m.TreasureBox_ResourceId,
			}).ToList();

			var resp = new TreasureboxListSelectResp
			{
				mapId = req.mapId,
				treasureboxListSelectItemInfoList = treasureboxListSelectItemInfoList
			};
			session.Send(defCommand, req, resp);
		}
	}
}
